﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Debug;
using Microsoft.Xna.Framework.Content;
using Kristall.MyGame.Scripts;
using System.Reflection;
using System.ComponentModel;

namespace Kristall.Engine
{
    public class Animatable : AABB
    {
        [Category("Animations")]
        public List<AnimatableSprite> Animations
        {
            get;
            set;
        }

        Level _level;

        [Category("Animations"), DescriptionAttribute("0 = Front, 1 = Back")]
        public float LayerDepth
        {
            get;
            set;
        }

        [Browsable(false), XmlIgnore]
        public Level Level
        {
            get
            {
                return _level;
            }
            set
            {
                _level = value;
            }
        }

        private string currentAnimationKey = "";

        [Category("Animations")]
        public string CurrentAnimationKey
        {
            get
            {
                return currentAnimationKey;
            }
            set
            {
                if (Animations.FirstOrDefault(x => x.FileName == value) != null)
                {
                    currentAnimationKey = value;
                }
                else
                {
                    Kristall.Echo(DebugConsole.CCRed + "Animation doesn't exist: " + value);
                }
            }
        }

        [XmlIgnore, Browsable(false)]
        public AnimatableSprite CurrentAnimation
        {
            get
            {
                return Animations.FirstOrDefault(x => x.FileName == currentAnimationKey);
            }
        }

        public Animatable() : base()
        {
            Animations = new List<AnimatableSprite>();
            LayerDepth = 1;
        }

        public override void LoadContent(Level level)
        {
            foreach (AnimatableSprite animation in Animations)
            {
                animation.LoadContent(level.SpriteTextures, level.Content);
            }

            base.LoadContent(level);

            Level = level;
        }

        public override void RemoveFromLevel()
        {
            Level.Animatables.Remove(this);
        }

        public override void AddToLevel()
        {
            if (this.Id.Equals("Id_not_set"))
                this.Id = "Animatable" + Level.Animatables.Count;

            Level.Animatables.Add(this);
        }


        public override void Update(float dt)
        {
            if (CurrentAnimation != null)
            {
                CurrentAnimation.Update(dt);
            }

            base.Update(dt);
        }

        public virtual void Draw(SpriteBatch batch)
        {
            if (CurrentAnimation != null)
            {
                CurrentAnimation.Draw(batch, this.Position, LayerDepth);
            }

            base.DrawDebug(batch);
        }
    }
}
